<template>
  <div class="about">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索与添加 -->
      <el-form :inline="true" ref="query" :model="query">
        <el-form-item prop="createBy">
          <el-input
            v-model="query.createBy"
            style="width: 150px"
            placeholder="发布人"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="state">
          <el-select
            v-model="query.state"
            style="width: 150px"
            placeholder="发布状态"
            clearable
          >
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="未发布" value="未发布"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" size="medium" @click="resetForm('query')" round
            >重置</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button @click="getList" type="primary" size="medium" round
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="4"
          ><el-button @click="addDialogVisible = true" type="success"
            >创建公告</el-button
          ></el-col
        >
      </el-row>
      <!-- 公告信息列表 -->
      <el-table border stripe :data="list">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="级别" prop="level"> </el-table-column>
        <!-- <template slot-scope="scope">
          <el-tag v-if="scope.row.level === '0'">一般</el-tag>
          <el-tag type="danger" v-else-if="scope.row.level === '1'"
            >紧急</el-tag
          >
          <el-tag type="warning" v-else>重要</el-tag>
        </template> -->
        <el-table-column label="标题" prop="title"> </el-table-column>
        <el-table-column label="发布日期" prop="date"> </el-table-column>
        <el-table-column label="发布人" prop="createBy"> </el-table-column>
        <el-table-column label="发布状态" prop="state"> </el-table-column>
        <!-- <el-table-column label="置顶" prop="isTopping"> </el-table-column> -->
        <el-table-column label="公告内容" prop="content" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" width="210px">
          <template slot-scope="scope">
            <el-button type="success" size="mini" @click="fbu(scope.row.id)"
              >发布</el-button
            >
            <el-button
              @click="showEdit(scope.row.id)"
              type="primary"
              size="mini"
              >更新</el-button
            >
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currentPage"
        :page-sizes="[4, 8, 15, 100]"
        :page-size="query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加公告 -->
      <el-dialog
        title="添加公告信息"
        :visible.sync="addDialogVisible"
        width="32%"
        @close="addDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="级别" prop="level">
            <el-select
              v-model="addForm.level"
              style="width: 265px"
              placeholder="请选择级别"
            >
              <el-option label="一般" value="一般"></el-option>
              <el-option label="紧急" value="紧急"></el-option>
              <el-option label="重要" value="重要"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="addForm.title"></el-input>
          </el-form-item>
          <el-form-item label="发布人" prop="createBy">
            <el-input v-model="addForm.createBy"></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="content">
            <el-input
              v-model="addForm.content"
              type="textarea"
              autosize
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addAnnouncement">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 更新公告信息 -->
      <el-dialog
        title="更新公告信息"
        :visible.sync="editDialogVisible"
        width="35%"
        @close="editDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="100px"
        >
          <el-form-item label="级别" prop="level">
            <el-select
              v-model="editForm.level"
              style="width: 265px"
              placeholder="请选择级别"
            >
              <el-option label="一般" value="一般"></el-option>
              <el-option label="紧急" value="紧急"></el-option>
              <el-option label="重要" value="重要"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="editForm.title"></el-input>
          </el-form-item>
          <el-form-item label="发布人" prop="createBy">
            <el-input v-model="editForm.createBy"></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="content">
            <el-input v-model="editForm.content"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editsubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: {
        currentPage: 1,
        size: 4,
        createBy: "",
        state: "",
      },
      list: [],
      total: 0,
      addDialogVisible: false,
      addForm: {
        level: "",
        title: "",
        date: "",
        createBy: "",
        state: "",

        content: "",
      },
      addFormRules: {
        level: [{ required: true, message: "请选择级别", trigger: "blur" }],
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "标题为1~50个字符之间",
            trigger: "blur",
          },
        ],
        createBy: [
          { required: true, message: "请输入发布人名称", trigger: "blur" },
          {
            min: 2,
            max: 5,
            message: "发布人名称为2~5个字符之间",
            trigger: "blur",
          },
        ],

        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "公告内容为1~500个字符之间",
            trigger: "blur",
          },
        ],
      },
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        level: [
          {
            required: true,
            message: "请输入级别",
            trigger: "blur",
          },
        ],
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "标题为1~50个字符之间",
            trigger: "blur",
          },
        ],
        createBy: [
          { required: true, message: "请输入发布人名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "发布人名称为2~50个字符之间",
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "公告内容为1~500个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
     //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取公告信息
    async getList() {
      await this.$http
        .get("announcement/announcementQuery", { params: this.query })
        .then((res) => {
          console.log(res);
          if (res.data.flag !== true) {
            return this.$message.error("获取信息失败!");
          }
          this.list = res.data.rows;
          this.total = res.data.total;
        });
    },
    //监听信息条数
    handleSizeChange(newSize) {
      this.query.size = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.query.currentPage = newPage;
      this.getList();
    },
    //监听添加公告表单的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮添加公告
    addAnnouncement() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起添加公告的网络请求
        const { data: res } = await this.$http.post(
          "announcement/addAnnouncement",
          this.addForm
        );
        console.log(res);
        if (res.flag !== true) {
          return this.$message.error("添加公告信息失败!");
        }
        this.$message.success("添加公告信息成功!");
        //隐藏添加公告信息的对话框
        this.addDialogVisible = false;
        //重新获取公告信息列表数据
        this.getList();
      });
    },
    //监听更新公告表单的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //根据id查询公告信息
    async showEdit(id) {
      // console.log(id);
      const { data: res } = await this.$http.get(
        "announcement/announcementQueryById",
        { params: { id } }
      );
      console.log(res);
      if (res.flag !== true) {
        return this.$message.error("获取公告信息失败!");
      }
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    //更新公告信息
    editsubmit() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起更新公告的网络请求
        const { data: res } = await this.$http.post(
          "announcement/updateAnnouncement",
          {
            id: this.editForm.id,
            level: this.editForm.level,
            title: this.editForm.title,
            createBy: this.editForm.createBy,
            content: this.editForm.content,
          }
        );
        if (res.flag !== true) {
          return this.$message.error("更新公告信息失败!");
        }
        this.$message.success("更新公告信息成功!");
        //隐藏更新公告信息的对话框
        this.editDialogVisible = false;
        //重新获取公告信息列表数据
        this.getList();
      });
    },
    //删除公告信息
    async removeId(id) {
      console.log(id);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该公告, 是否继续?",
        "删除公告信息",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post(
        "announcement/deleteAnnouncement/",
        {
          id: id,
        }
      );
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除公告信息信息失败");
      }
      this.$message.success("删除公告信息信息成功");
      this.getList();
    },
    //发布公告接口
    async fbu(id) {
      const { data: res } = await this.$http.get(
        "/announcement/publishAnnouncement",
        { params: { id } }
      );
      console.log(res);
      if (res.message !== "发布成功") {
        return this.$message.error("发布公告信息失败!");
      }
      this.$message.success("公告信息信息发布成功");
      this.getList();
    },
  },
};
</script>

<style></style>
